﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AutoWelding.DAL;
using AutoWelding.Models;

namespace AutoWelding.BLL
{
    public class RecipesBLL
    {
        private RecipesDAL recipesDAL = new RecipesDAL();
        private ProductsDAL productsDAL = new ProductsDAL();
        /// <summary>
        /// 获取配方列表
        /// </summary>
        /// <param name="recipeName"></param>
        /// <param name="productId"></param>
        /// <param name="isDelete"></param>
        /// <returns></returns>
        public List<RecipesModel> GetRecipesList(string recipeName, int productId, bool isDelete)
        {
            int isDeletedCode = isDelete ? 0 : 1;
            List<RecipesModel> recipes = recipesDAL.GetRecipesList(recipeName, productId, isDeletedCode);
            foreach (var recipesModel in recipes)
            {
                ProductsModel productsModel = productsDAL.GetProductById(recipesModel.ProductId);
                recipesModel.ProductName = productsModel.ProductName;
            }
            return recipes;
            
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="selectedRecipeIds"></param>
        /// <returns></returns>
        public bool LogicDeleteRecipes(List<int> selectedRecipeIds)
        {
            if (selectedRecipeIds == null || selectedRecipeIds.Count == 0)
            {
                return false;
            }
            // 调用 DAL 层批量更新方法
            int affectedRows = recipesDAL.BatchUpdateIsDelete(selectedRecipeIds, 0);
            return affectedRows > 0;
        }

        /// <summary>
        /// 修改配方
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateRecipe(RecipesModel model)
        {
            return recipesDAL.UpdateRecipe(model) > 0;
        }

        /// <summary>
        /// 恢复配方
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool RecoverRecipe(List<int> selectedRecipeIds)
        {
            return recipesDAL.BatchUpdateIsDelete(selectedRecipeIds, 1)>0;
        }

        /// <summary>
        /// 真实删除配方
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteRecipe(int id)
        {
            return recipesDAL.DeleteRecipe(id) > 0;
        }
        /// <summary>
        /// 根据id获取配方信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RecipesModel GetRecipesById(int id)
        {
            return recipesDAL.GetRecipeById(id);
        }
        /// <summary>
        /// 添加配方
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool InsertRecipe(RecipesModel model)
        {
            return recipesDAL.InsertRecipe(model) > 0;
        }
    }
}
